package com.pig4cloud.pigx.contract.utils;

import com.pig4cloud.pigx.common.customize.i18n.util.MessageUtils;
import com.pig4cloud.pigx.contract.constant.I18nMessages;
import com.pig4cloud.pigx.contract.enums.ContractOrderEnum;
import com.pig4cloud.pigx.contract.enums.ContractPositionEnum;
import org.springframework.stereotype.Component;

import java.util.Locale;
import java.util.Objects;

/**
 * 合约模块国际化工具
 *
 * @since 2022/9/27 14:56
 */
@Component
public class ContractMessageUtils {
	/**
	 * 当前用户的语言环境
	 */
	public static final ThreadLocal<Locale> LOCALE = new ThreadLocal<>();

	/**
	 * 设置语言环境
	 *
	 * @param languageCode 语言环境代码
	 * @since 2022/9/27 14:59
	 */
	public static void setLocale(String languageCode) {
		// 获取语言环境
		Locale locale = MessageUtils.getLocale(languageCode);
		LOCALE.set(locale);
	}

	/**
	 * 检查语言环境
	 *
	 * @since 2022/9/27 15:09
	 */
	public static void checkLocale() {
		if (Objects.isNull(LOCALE.get())) {
			throw new RuntimeException("语言环境数据异常");
		}
	}

	/**
	 * 仓位模式
	 *
	 * @param positionMode 仓位模式
	 * @return {@link String}
	 * @since 2022/9/27 15:00
	 */
	public static String positionMode(String positionMode) {
		// 检查语言环境
		checkLocale();

		// 翻译仓位模式
		String positionModeKey = Objects.equals(ContractPositionEnum.Mode.FULL.getValue(), positionMode) ?
				I18nMessages.POSITION_MODE_FULL
				: I18nMessages.POSITION_MODE_ISOLATE;
		return MessageUtils.getMessageByLocale(positionModeKey, LOCALE.get());
	}

	/**
	 * 订单方向
	 *
	 * @param orderSide 订单方向
	 * @return {@link String}
	 * @since 2022/9/27 15:00
	 */
	public static String orderSide(String orderSide) {
		// 检查语言环境
		checkLocale();

		// 翻译订单方向
		String sideKey = Objects.equals(ContractOrderEnum.Side.MORE.getValue(), orderSide) ?
				I18nMessages.ORDER_SIDE_LONG
				: I18nMessages.ORDER_SIDE_SHORT;
		return MessageUtils.getMessageByLocale(sideKey, LOCALE.get());
	}
}
